package liecai_class.day06;

import java.util.concurrent.Semaphore;

/**
 * 信号量 Semaphore
 * 1.保护一段重要的代码一次不超过n个线程访问
 * 2.在两个线程之间发送信号
 */
public class Demo10 {
    private static Semaphore semaphore = new Semaphore(0);
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("卡车司机等待装货...");
                    //---等待信号
                    semaphore.acquire();
                    System.out.println("卡车司机收到信号，一脚油门，烧胎起步...");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("##装卸师傅在装货...");
                    Thread.sleep(3000);
                    System.out.println("##装卸师傅给卡车司机发一个信号，告知装货完成可以出发..");
                    //--发送信号
                    semaphore.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
